//package com.example.springai.controller;
//
//import com.example.springai.config.AIConfig;
//import com.example.springai.config.ChatRequest;
//import com.example.springai.config.ChatRequestExample;
//import org.springframework.ai.chat.messages.UserMessage;
//import org.springframework.ai.model.function.FunctionCallback;
//import org.springframework.ai.model.tool.ToolCallingChatOptions;
//import org.springframework.ai.openai.api.OpenAiApi;
//
//import org.springframework.ai.tool.ToolCallback;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.lang.reflect.InvocationTargetException;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@RestController
//@RequestMapping("/openai/functionCallingSchool")
//public class FunctionCallingSchoolController {
//
//    @Autowired
//    AIConfig chatRequestBean;
//
//
//    @GetMapping(value = "/deepseek/functionCalling/v3")
//public Map<String, String> functionCallingv31(@RequestParam(value = "message", defaultValue = "查询清华大学信息") String message) {
//    UserMessage userMessage = new UserMessage(message);
//    final String[] body1 = new String[1];
//    Map<String, String> bodyMap = new HashMap<>();
//    body1[0] = "系统出现BUG，请稍后再试";
//    ChatRequest.Message system = new ChatRequest.Message("system", "你是公司内部的AI助手用户提问的内容，如果和函数无关，请不要调用函数，请格式化输出内容到html的<body>标签中");
//    ChatRequest.Message user = new ChatRequest.Message("user", message);
//    try {
//
//        OpenAiApi.ChatCompletionRequest currentWeather =  chatRequestBean.createChatRequestFunction1();
//        List<OpenAiApi.FunctionTool> tools = currentWeather.tools();
//        System.out.println("tools = " + tools);
//        org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionChunkResponseEntity = ChatRequestExample.requestDeepSeekChat(tools, List.of(system, user));
//
////        getRegisteredFunctions();
//
//        Map<String, ToolCallback> functionCallbackRegister = chatRequestBean.getFunctionCallbacks();
//
//        OpenAiApi.ChatCompletion body = chatCompletionChunkResponseEntity.getBody();
//        if (body != null) {
//            {
//                List<OpenAiApi.ChatCompletionMessage.ToolCall> toolCalls = body.choices().get(0).message().toolCalls();
//                if (toolCalls == null || toolCalls.isEmpty()) {
//                    body1[0] = body.choices().get(0).message().content();
//
//                    bodyMap.put("content", body1[0]);
//                    return bodyMap;
//                }
//                body.choices().forEach(choice -> {
//                    List<OpenAiApi.ChatCompletionMessage.ToolCall> toolCalls1 = choice.message().toolCalls();
//                    //TODO 管理添加的功能，需要人工二次确认才可行
//                    Iterator<OpenAiApi.ChatCompletionMessage.ToolCall> iterator = toolCalls1.iterator();
//                    while (iterator.hasNext()) {
//                        OpenAiApi.ChatCompletionMessage.ToolCall toolCall = iterator.next(); // 获取当前元素
//                        FunctionCallback functionCallback = functionCallbackRegister.get(toolCall.function().name());
//                        String call = functionCallback.call(toolCall.function().arguments());
//                        System.out.println("call = " + call);
//                        // 如果不是最后一个元素，继续迭代
//                        if (iterator.hasNext()) {
//
//                        } else {
//                            // 如果是最后一个元素，执行额外的逻辑
//                            ResponseEntity<OpenAiApi.ChatCompletion>
//                                    chatCompletionChunkResponseEntity1 = ChatRequestExample.requestDeepSeekChat(null, List.of(
//                                    system,
//                                    user,
//                                    new ChatRequest.Message("system", call)
//                            ));
//                            body1[0] = chatCompletionChunkResponseEntity1.getBody().choices().get(0).message().content();
//                            bodyMap.put("content", body1[0]);
//                        }
//                    }
//                });
//            }
//        }
//        return bodyMap;
//    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
//        throw new RuntimeException(e);
//    } catch (Exception e) {
//        throw new RuntimeException(e);
//    }
//}
//
////    public void getRegisteredFunctions() {
////        // 假设 ToolCallbackProvider 有一个 getToolCallbacks 方法来获取所有的 ToolCallback
////        FunctionCallback[] toolCallbacks = manualToolExecutor.getToolCallbacks();
////        for (FunctionCallback toolCallback : toolCallbacks) {
////            // 获取函数名称
////            String functionName = toolCallback.getName();
////            System.out.println("Registered function: " + functionName);
////        }
////    }
//}
